package sdc.media.motorbike.View;

import sdc.media.motorbike.MotorBike;
import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class LoseScreen extends BaseScreen {

	// Background and button, etc...
	private Image imgBack;
	private float sizeX = Gdx.graphics.getWidth() / 480.0f;
	private float sizeY = Gdx.graphics.getHeight() / 800.0f;
	
	private TextField strName;
	private String label;
	private Dialog dialog;
	private TextButton btnYes;
	private TextButton btnCancel;
	private Skin skin = new Skin(Gdx.files.internal("data/uiskin/uiskin.json"));

	@Override
	public void render(float delta) {
		super.render(delta);
		batch.begin();
		Assets.font_1.setScale(sizeX, sizeY);
		Assets.font_1.draw(batch, "GAME OVER", 165 * sizeX, 700 * sizeY);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void show() {
		super.show();
		
		// Constructor
		imgBack = new Image(Assets.backgroundMenu);
		imgBack.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		stage.addActor(imgBack);
		
		if (Setting.soundEnabled) {
			// Play music and sound
			Assets.backgroundPlayMusic.play();
		} else {
			//Pause music and sound
			Assets.backgroundPlayMusic.pause();
		}

		inputName("Lưu điểm số");
	}
	
	private void inputName(String title) {

		label = "  Số điểm của bạn là: " + Setting.score
				+ "\n  Bạn có muốn lưu lại không?  \n\n\n\n\n\n";

		dialog = new Dialog(title, skin);

		strName = new TextField("", skin);
		strName.setColor(Color.LIGHT_GRAY);
		strName.setWidth(245f);
		strName.addListener(new EventListener() {
			@Override
			public boolean handle(Event event) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		strName.setPosition(15, 85);

		strName.setTextFieldListener(new TextFieldListener() {
			public void keyTyped(TextField textField, char key) {
				if (key == '\n')
					textField.getOnscreenKeyboard().show(false);
			}
		});

		dialog.addActor(strName);
		dialog.text(label).show(stage);

		btnYes = new TextButton("Có", skin);
		btnYes.setPosition(55, 20);
		btnYes.setSize(75, 45);
		dialog.addActor(btnYes);

		btnCancel = new TextButton("Không", skin);
		btnCancel.setPosition(145, 20);
		btnCancel.setSize(75, 45);
		dialog.addActor(btnCancel);

		// Nut xac nhan Yes
		btnYes.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				Assets.playSound(Assets.buttonClick);
				String name = strName.getText().toString();
				strName.getOnscreenKeyboard().show(false);
				dialog.hide();
				Setting.addValue(name, Setting.score);
				Setting.save();
				Setting.restart();
				MotorBike.getManagerScreen().createScreen(ManagerScreen.SCREEN_HIGH_SCORE);
			}

		});

		// Nut xac nhan No
		btnCancel.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				super.touchUp(event, x, y, pointer, button);
				Assets.playSound(Assets.buttonClick);
				dialog.hide();
				Setting.restart();
				MotorBike.getManagerScreen().createScreen(ManagerScreen.SCREEN_MENU);
			}
		});

		dialog.show(stage);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
	}

}
